package com.test2;

import org.asynchttpclient.util.Assertions;
import org.openqa.selenium.By;
import org.openqa.selenium.bidi.LogInspector;
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
import org.openqa.selenium.bidi.browsingcontext.NavigationResult;
import org.openqa.selenium.bidi.log.JavascriptLogEntry;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class YXMUtil3 {

    public static void main(String[] args) {
        String path = "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe";
        if(args.length > 0){
            path = args[0];
        }
        // 参数配置
        System.setProperty("webdriver.chrome.driver", path);
        ChromeOptions option = new ChromeOptions();
//        option.addArguments("headless"); // 无界面参数
//        option.addArguments("no-sandbox"); // 禁用沙盒
        // 通过ChromeOptions的setExperimentalOption方法，传下面两个参数来禁止掉谷歌受自动化控制的信息栏
        //option.setExperimentalOptions("useAutomationExtension", false);
        //option.setExperimentalOptions("excludeSwitches", Collections.singletonList("enable-automation"));
        //设置为 headless 模式 （必须）
        //option.addArguments("--headless");
        option.addArguments("--disable-gpu");//谷歌文档提到需要加上这个属性来规避bug
        option.addArguments("--no-sandbox");// “–no - sandbox”参数是让Chrome在root权限下跑
        option.addArguments("--disable-dev-shm-usage");
        option.addArguments("lang=zh_CN.UTF-8");
        option.setCapability("webSocketUrl", true);
        //option.addArguments("window-size=1920x1080"); //指定浏览器分辨率
        ChromeDriver driver = new ChromeDriver(option);


        try (LogInspector logInspector = new LogInspector(driver)) {
            CompletableFuture<JavascriptLogEntry> future = new CompletableFuture<>();
            logInspector.onJavaScriptException(future::complete);

            BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());

            NavigationResult info =
                    browsingContext.navigate("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");

//            Assertions.assertNotNull(browsingContext.getId());
//            Assertions.assertNull(info.getNavigationId());
//            Assertions.assertTrue(info.getUrl().contains("/bidi/logEntryAdded.html"));

            new WebDriverWait(driver, Duration.ofSeconds(10))
                    .until(ExpectedConditions.elementToBeClickable(By.id("jsException"))).click();

            JavascriptLogEntry logEntry = null;
            try {
                logEntry = future.get(5, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            } catch (TimeoutException e) {
                throw new RuntimeException(e);
            }

            Assertions.assertNotNull("Error: Not working", logEntry.getText());
            Assertions.assertNotNull("javascript", logEntry.getType());
        }


    }

}

